﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Class_Entity;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess
{
    public class DA__Place
    {
        SqlConnection CON = new SqlConnection();
        SqlCommand CMD;
        string connectionString;

        public DataSet Load_Place(CE__Place ce)
        {
            DataSet DS = new DataSet();
            try
            {
                CON = ConnectionToDatabase.StartConnection();
                CON.Open();

                CMD = new SqlCommand("spGetAllPlace", CON);
                CMD.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(CMD);

                CMD.ExecuteNonQuery();
                data.Fill(DS);
                CON.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return DS;
        }

        public bool AddNew_Place(CE__Place ce)
        {
            try
            {
                
                CON = ConnectionToDatabase.StartConnection();
                CON.Open();
                CMD = new SqlCommand("spAddNewPlace", CON);
                CMD.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DATA = new SqlDataAdapter(CMD);

                CMD.Parameters.AddWithValue("@PlaceName", ce._PlaceName);

                CMD.ExecuteNonQuery();
                CON.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDatabase.CloseConnection();
            }

            return true;
        }

        public bool Update_Place(CE__Place ce)
        {
            try
            {
                
                CON = ConnectionToDatabase.StartConnection();
                CON.Open();
                CMD = new SqlCommand("spUpdatePlace", CON);
                CMD.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DATA = new SqlDataAdapter(CMD);

                CMD.Parameters.AddWithValue("@PlaceID", ce._PlaceID);
                CMD.Parameters.AddWithValue("@PlaceName", ce._PlaceName);

                CMD.ExecuteNonQuery();
                CON.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDatabase.CloseConnection();
            }

            return true;
        }

        public bool Delete_Place(CE__Place ce)
        {
            try
            {
                
                CON = ConnectionToDatabase.StartConnection();
                CON.Open();
                CMD = new SqlCommand("spDeletePlace", CON);
                CMD.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DATA = new SqlDataAdapter(CMD);

                CMD.Parameters.AddWithValue("@PlaceID", ce._PlaceID);

                CMD.ExecuteNonQuery();
                CON.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDatabase.CloseConnection();
            }

            return true;
        }
    }
}
